<?php
	class BoardMinutes implements AwispPlugin{
		public function __construct(){
			return;
		}
		
		public function init($baseFile){
			register_activation_hook($baseFile, array(&$this, "activate"));
			register_deactivation_hook($baseFile, array(&$this, "deactivate"));
			register_uninstall_hook($baseFile, array(get_class($this), "uninstall"));
			add_action("plugins_loaded", array(&$this, "loadTranslations"));
			add_action("init", array(&$this, "registerPostType"));
			add_action("admin_menu", array(&$this, "createSubscriberMenu"));
		}
		
		public function activate(){
			$this->registerPostType();
			flush_rewrite_rules();
		}
		
		public function deactivate(){
			flush_rewrite_rules();
		}
		
		public function uninstall(){
			$args = array(
				"posts_per_page" => "-1",
				"post_type" => "board-minute",
				"post_status" => "any"
			);
			
			$posts = get_posts($args);
			foreach($posts as $post){
				wp_delete_post($post->ID);
			}
		}
		
		public function loadTranslations(){
			load_plugin_textdomain(BOARDMINUTES_TEXT_DOMAIN, false, basename(BOARDMINUTES_BASE_PATH) . "/" . basename(BOARDMINUTES_LANGUAGES_PATH));
		}
		
		public function shortcode(){
			return;
		}
		
		public function registerPostType(){
			$labels = array(
				"name" => __("Board Minutes", BOARDMINUTES_TEXT_DOMAIN),
				"singular_name" => __("Board Minute", BOARDMINUTES_TEXT_DOMAIN),
				"menu_name" => __("Board Minutes", BOARDMINUTES_TEXT_DOMAIN),
				"name_admin_bar" => __("Board Minute", BOARDMINUTES_TEXT_DOMAIN),
				"add_new" => __("Add New", BOARDMINUTES_TEXT_DOMAIN),
				"add_new_item" => __("Add New Board Minute", BOARDMINUTES_TEXT_DOMAIN),
				"new_item" => __("New Board Minute", BOARDMINUTES_TEXT_DOMAIN),
				"edit_item" => __("Edit Board Minute", BOARDMINUTES_TEXT_DOMAIN),
				"view_item" => __("View Board Minute", BOARDMINUTES_TEXT_DOMAIN),
				"all_items" => __("All Board Minutes", BOARDMINUTES_TEXT_DOMAIN),
				"search_items" => __("Search Board Minutes", BOARDMINUTES_TEXT_DOMAIN),
				"parent_item_colon" => __("Parent Board Minutes:", BOARDMINUTES_TEXT_DOMAIN),
				"not_found" => __("No Board Minutes found.", BOARDMINUTES_TEXT_DOMAIN),
				"not_found_in_trash" => __("No Board Minutes found in Trash.", BOARDMINUTES_TEXT_DOMAIN)
			);
			
			$supports = array("title", "editor", "revisions");
			
			$rewrite = array(
				"slug" => "zapis-z-vyboru"
			);
			
			$args = array(
				"labels" => $labels,
				"public" => true,
				"exclude_from_search" => true,
				"publicly_queryable" => true,
				"show_ui" => true,
				"show_in_nav_menus" => true,
				"show_in_menu" => true,
				"show_in_admin_bar" => true,
				"menu_position" => 100,
				"supports" => $supports,
				"rewrite" => $rewrite
			);
			
			register_post_type("board-minute", $args);
		}
		
		public function createSubscriberMenu(){
			$currentUser = wp_get_current_user();
			if(!(array_search("subscriber", $currentUser->roles) === false)){
				add_menu_page(__("Board Minutes", BOARDMINUTES_TEXT_DOMAIN), __("Board Minutes", BOARDMINUTES_TEXT_DOMAIN), "read", BOARDMINUTES_TEXT_DOMAIN, array(&$this, "generateContent"));
			}
		}
		
		public function generateContent(){
			$currentPage = max(1, isset($_GET["paged"])? absint($_GET["paged"]): 0);
			$orderBy = $this->getRequestVariable("orderby", array("title", "date"), "date");
			$order = $this->getRequestVariable("order", array("asc", "desc"), "desc");
			
			$args = array(
				"posts_per_page" => BOARDMINUTES_PER_PAGE,
				"offset" => ($currentPage - 1) * BOARDMINUTES_PER_PAGE,
				"orderby" => $orderBy,
				"order" => strtoupper($order),
				"post_type" => "board-minute",
				"post_status" => "publish",
				"suppress_filters" => true
			);
			
			$posts = get_posts($args);
			$view = new BoardMinutesView($posts, $orderBy, $order, $currentPage);
			echo $view->getContent();
		}
		
		private function getRequestVariable($variableName, $allowedValues, $defaultValue){
			if(isset($_GET[$variableName])){
				foreach($allowedValues as $allowedValue){
					if($_GET[$variableName] == $allowedValue){
						return $_GET[$variableName];
					}
				}
			}
			return $defaultValue;
		}
	}
?>